﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LeadVinaglue.Models;
using LeadVinaglue.Models.ViewPages;
using System.Web.Mvc;

namespace LeadVinaglue.Repository
{
    public class LanguageManagementRepository : BaseManagementRepository
    {
        LeadVinaglueEntities entities = new LeadVinaglueEntities();

        public LanguageManagementModel GetModel(string pageType, string roleName)
        {
            LanguageManagementModel model = new LanguageManagementModel();

            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);
            model.ListLanguage = GetAll();

            return model;
        }

        public LanguageManagementModel GetModelAdd(string pageType, string roleName)
        {
            LanguageManagementModel model = new LanguageManagementModel();

            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);
            model.ListState = new Repository.StateRepository().GetAll();

            return model;
        }

        public LanguageManagementModel GetModelEdit(string pageType, string roleName, int id)
        {
            LanguageManagementModel model = new LanguageManagementModel();

            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);
            model.ListState = new Repository.StateRepository().GetAll();

            if (id > 0)
            {
                model.LanguageInfo = GetSingle(id);
            }

            return model;
        }

        public LanguageManagementModel GetModelDelete(string pageType, string roleName)
        {
            LanguageManagementModel model = new LanguageManagementModel();
            model.ListLanguage = GetAll();

            return model;
        }

        public List<Language> GetAll()
        {
            List<Language> list = new List<Language>();

            var items = (from c in entities.Languages
                         orderby c.LanguageId ascending
                         select c).ToList();

            foreach (var item in items)
            {
                Language model = new Language();

                model.LanguageId = item.LanguageId;
                model.Title = item.Title;
                model.EntityKey = item.EntityKey;
                model.Visible = item.Visible;
                list.Add(model);
            }

            return list;
        }

        public Language GetSingle(int id)
        {
            return (from _object in entities.Languages where _object.LanguageId == id select _object).FirstOrDefault();
        }

        public void Create([Bind(Exclude = "ID, DateCreated")] Language _object)
        {
            //_object.DateCreated = System.DateTime.Now;
            entities.AddToLanguages(_object);
            entities.SaveChanges();
        }

        public void Delete(int id)
        {
            var _object = (from _list in entities.Languages where _list.LanguageId == id select _list).First();
            entities.DeleteObject(_object);
            entities.SaveChanges();
        }

        public void Save([Bind(Exclude = "ID")] Language _object)
        {
            //_object.DateCreated = System.DateTime.Now;
            entities.SaveChanges();
        }
    }
}